package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.BeanUtils.MyBeanUtils;
import com.sky.QueryByPage.DishQueryByPage;
import com.sky.dto.DishDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishFlavorVo;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class DishServiceimpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Override
    public PageResult getByPage(DishQueryByPage dqbg) {

        PageHelper.startPage(dqbg.getPage(),dqbg.getPageSize());
        List<Dish> dishes =  dishMapper.getByPage(dqbg);
//        List<DishVO> dishVos = MyBeanUtils.ListBeanUtils(dishes,DishVO::new);
        Page<Dish> p = (Page<Dish>) dishes;
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public DishVO getById(Integer id) {
        Dish dish =  dishMapper.getById(id);
        List<DishFlavor> dishFlavors =  dishFlavorMapper.getByDishId(id);
        List<DishFlavorVo> dishFlavorVos = MyBeanUtils.ListBeanUtils(dishFlavors, DishFlavorVo::new);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorVos);
        return dishVO;
    }

    @Transactional
    @Override
    public void update(DishDTO dto) {
        Dish dish = new Dish();
        dish.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(dto,dish);
        dishMapper.update(dish);

        dishFlavorMapper.deleteOldFlavor(dto.getId());

        List<DishFlavor> flavors = dto.getFlavors();

        Long id = dish.getId();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        dishFlavorMapper.add(flavors);

    }

    @Transactional
    @Override
    public void delete(List<Integer> ids) {
        dishMapper.delete(ids);
        dishFlavorMapper.delete(ids);
    }

    @Transactional
    @Override
    public void add(DishDTO dto) {
        Dish dish = new Dish();
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateTime(LocalDateTime.now());
        BeanUtils.copyProperties(dto,dish);
        dishMapper.add(dish);
        List<DishFlavor> flavors = dto.getFlavors();

        Long id = dish.getId();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        dishFlavorMapper.add(flavors);

    }

    @Override
    public void enable(Integer status, Integer id) {
        dishMapper.enable(status,id);
    }


    @Override
    public List<DishVO> getList(Integer id) {
        List<Dish> dish = dishMapper.getList(id);
        List<DishVO> dishVOS = MyBeanUtils.ListBeanUtils(dish, DishVO::new);
        return dishVOS;
    }


}
